package com.flyduck.utils;

import cn.hutool.core.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: vue-admin-java
 * @description:
 * @author: flyduck
 * @create: 2024-06-01 09:46
 **/
public class BeanUtils extends org.springframework.beans.BeanUtils {

    public static  <T> T toBean(Object source,Class<T> clazz){
        if (source == null) {
            return null;
        }

        T target = null;
        try {
            target = clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("bean拷贝出现异常", e);
        }
        copyProperties(source, target);
        return target;
    }

    public static  <S,T> List<T> toBeanList(List<S> sourceList, Class<T> clazz){
        List<T> targetList = new ArrayList<>();
        if (CollectionUtil.isEmpty(sourceList)) {
            return targetList;
        }
        for (Object source : sourceList) {
            T target = toBean(source, clazz);
            targetList.add(target);
        }
        return targetList;
    }
}
